//引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
//引入VueRouter
import VueRouter from 'vue-router'
//引入路由器
import router from './router'
// 引入ElementUI组件库
import ElementUI from 'element-ui'
// ElementUI全部样式
import 'element-ui/lib/theme-chalk/index.css'
// 引入Cookie
import Cookies from 'js-cookie'
import { handleTree } from '@/utils/ruoyi'
import Treeselect from '@riophae/vue-treeselect'
 
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
Vue.component('Treeselect',Treeselect)
//关闭Vue的生产提示
Vue.config.productionTip = false
//应用插件
Vue.use(VueRouter)
// 应用ElementUI
Vue.use(ElementUI)
Vue.prototype.handleTree = handleTree
// 添加全局路由前置守卫
router.beforeEach((to, from, next) => {
  // 判断token是否存在
  const token = Cookies.get('token')
  // token不存在,说明当前用户是未登录,应跳转到登录页面
  if (!token && to.name !== 'login') {
    next({
      name: 'login'
    })
    // token存在,说明当前用户已登录，此时跳转到首页
  } else if (token && to.name === 'login') {
    next({
      name: 'dataBoard'
    })
  } else {
    next()
  }
})

//创建vm
new Vue({
  el: '#app',
  render: h => h(App),
  router: router
})
